<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.enable" clearable class="filter-item" placeholder="请选择广告状态">
        <el-option v-for="(key,index) in statuss" :key="index" :label="key.name" :value="key.value" />
      </el-select>
      <el-select v-model="listQuery.type" clearable class="filter-item" placeholder="请选择广告类型">
        <el-option label="全部类型" value="" />
        <el-option v-for="(key,index) in types" :key="index" :label="key.name" :value="key.id" />
      </el-select>
      <el-button v-permission="['advert:list']" class="filter-item" type="primary" icon="el-icon-search" @click="search">查找</el-button>
      <el-button v-permission="['advert:add']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button v-permission="['advertType:list']" class="filter-item" icon="el-icon-link" type="success" plain @click="toAdvertType">广告类型</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中..." border fit highlight-current-row>
      <el-table-column align="center" width="80" label="广告ID" prop="id" />
      <el-table-column align="center" label="广告标题" prop="title" />
      <el-table-column align="center" label="广告类型" prop="typeName"/>
      <el-table-column align="center" label="广告状态" prop="enableName"/>
      <el-table-column align="center" label="排序" prop="seq"/>
      <el-table-column align="center" label="广告图片" prop="url">
        <template slot-scope="scope">
          <el-popover :hidden="!scope.row.img" placement="right" trigger="hover">
            <img :src="scope.row.url">
            <img slot="reference" :src="scope.row.url+'_60_60'" height="60" width="60" >
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="链接地址" prop="link"/>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['advert:detail']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['advert:delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="initList"/>

    <el-dialog :close-on-click-modal="false" :title="isAdd?'新增':'编辑'" :visible.sync="dialogFormVisible">
      <el-form v-loading="dialogLoading" ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="id" prop="id" hidden>
          <el-input v-model="dataForm.id" />
        </el-form-item>
        <el-form-item label="广告标题" prop="title">
          <el-input v-model="dataForm.title" />
        </el-form-item>
        <el-form-item label="广告类型" prop="type">
          <el-select v-model="dataForm.type" placeholder="请选择">
            <el-option v-for="(key, index) in types" :key="index" :label="key.name" :value="key.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="广告图片" prop="img">
          <uploader :file-list.sync="fileList" @success="uploadImgSuccess" @remove="removeImgSuccess"/>
        </el-form-item>
        <el-form-item label="广告状态" prop="status">
          <el-switch v-model="dataForm.enable" :active-value="true" :inactive-value="false" active-color="#13ce66"/>
        </el-form-item>
        <el-form-item label="链接地址" prop="link">
          <el-input v-model="dataForm.link" />
        </el-form-item>
        <el-form-item label="排序" prop="seq">
          <el-input v-model.number="dataForm.seq" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-permission="['advert:add']" v-if="isAdd" :loading="submitIng" type="primary" @click="createData">确定</el-button>
        <el-button v-permission="['advert:edit']" v-else :loading="submitIng" type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { AdvertService } from '@/api/PromotionService'
import Uploader from '@/components/Uploader'
import Pagination from '@/components/Pagination'
import Notify from '@/utils/notify'
const statuss = [{ value: '', name: '全部' }, { value: false, name: '禁用' }, { value: true, name: '启用' }]

export default {
  name: 'Advert',
  components: { Uploader, Pagination },
  data() {
    return {
      statuss, isAdd: true, types: [], dialogLoading: false, list: [], fileList: [], total: 0, listLoading: true,
      listQuery: { pageNo: 1, pageSize: 20, enable: undefined, type: undefined },
      dataForm: { seq: 0, id: undefined, type: undefined, title: undefined, img: undefined, enable: true, link: undefined }, dialogFormVisible: false,
      rules: {
        title: [{ required: true, message: '广告标题不能为空', trigger: 'blur' }],
        img: [{ required: true, message: '广告图片不能为空', trigger: 'blur' }],
        seq: [{ required: true, message: '排序不能为空', trigger: 'blur' }, { type: 'number', message: '必须是整数!', trigger: 'blur' }],
        type: [{ required: true, message: '请选择广告类型', trigger: 'blur' }]
      },
      submitIng: false
    }
  },
  created() {
    this.initList()
    this.loadAvertType()
  },
  methods: {
    initList() {
      this.listLoading = true
      AdvertService.listAdvert(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    loadAvertType() {
      AdvertService.getAllAdvertType().then(result => {
        this.types = result
      })
    },
    search() {
      this.listQuery.pageNo = 1
      this.initList()
    },
    resetForm() {
      this.fileList = []
      this.dataForm = { id: undefined, type: undefined, title: undefined, img: undefined, enable: true, link: undefined }
    },
    toAdvertType() {
      this.$router.replace({ path: '/promotion/advert/type' })
    },
    handleCreate() {
      this.resetForm()
      this.dialogFormVisible = true
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submitIng = true
          AdvertService.addAdvert(this.dataForm).then(() => {
            this.initList()
            this.dialogFormVisible = false
            this.submitIng = false
            Notify.success(this, '新增成功')
          })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogFormVisible = true
      this.isAdd = false
      if (this.dataForm.img) {
        const url = this.dataForm.url
        this.fileList = [{ name: url.substring(url.lastIndexOf('/')), url: url }]
      } else {
        this.fileList = []
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          AdvertService.editAdvert(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.initList()
            Notify.success(this, '更新成功')
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('您确认要删除该广告吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        AdvertService.deleteAdvert(row.id, row.type).then(() => {
          this.initList()
          Notify.success(this, '删除成功')
        })
      }).catch(() => {
        return false
      })
    },
    uploadImgSuccess(data) {
      this.dataForm.img = data.path
    },
    removeImgSuccess() {
      this.dataForm.img = undefined
    }
  }
}
</script>
